<template>
	<view>
		<view class="navBar">
			<view class="left">
				<uni-icons type="left" size="24" @click="toBack"></uni-icons>
			</view>
			<view class="center">用户登录</view>
			<view class="right"></view>
		</view>
		<view class="header">
			<view class="headText">
				<view class="login-cn">用户登录</view>
				<view class="login-en">Login</view>
			</view>
		</view>
		<view class="loginBox">
			<view class="userInput">
				<view class="">用户名:</view>
				<input type="text" v-model="user.username" placeholder="请输入用户名"/>
			</view>
			<view class="passwordInput">
				<view class="">密码:</view>
				<input type="password" v-model="user.password" placeholder="请输入密码"/>
			</view>
			<button class="login-btn" type="primary" @click="login">登录</button>
			<button class="register-btn" type="default" @click="toRegister">注册</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				user: {
					username: '',
					password: '',
				},
			}
		},
		onLoad() {},
		methods: {
			login() {
				uni.request({
					url: 'http://localhost:8080/login',
					method: 'POST',
					header: {
						'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
					},
					data: {
						username: this.user.username,
						password: this.user.password,
					},
					success: res => {
						if (res.data == "登录失败") {
							uni.showToast({
								icon:"error",
								title:res.data
							})
						} else{
							uni.setStorageSync("token", res.data);
							this.toHome();
						}
					},
					fail: () => {},
					complete: () => {}
				});
			},
			toRegister() {
				uni.navigateTo({
					url: '/pages/register/register',
				});
			},
			toBack(){
				uni.navigateBack({
					delta: 1,
				});
			},
			toHome(){
				uni.reLaunch({
					url:"/pages/home/home"
				})
			}
		}
	}
</script>

<style lang="scss">
	.navBar {
		display: flex;
		height: 100rpx;
		background-color: white;
		align-items: center;
		padding: 0 30rpx;
		/* #ifdef MP-WEIXIN */
		padding-top: 100rpx;
		/* #endif */
	
		.left {
			flex: 1;
		}
	
		.center {
			flex: 1;
			color: #646566;
			text-align: center;
			font-size: 34rpx;
			font-weight: 700;
		}
	
		.right {
			flex: 1;
		}
	}
	.header {
		margin: 160rpx 30rpx;
		.login-cn{
			margin-bottom: 30rpx;
			font-size: 52rpx;
			color: #646566;
			font-weight: 700;
		}
		.login-en {
			font-size: 38rpx;
			font-weight: 400;
			color: #83838b;
		}

	}
	.loginBox{
		margin: 0 50rpx;
		.userInput,.passwordInput{
			display: flex;
			align-items: center;
			padding: 30rpx 0;
			border-bottom: 1rpx solid #ccc;
			view{
				flex: 1;
			}
			input{
				flex: 3;
			}
		}
		.login-btn{
			margin-top: 200rpx;
			border-radius: 50rpx;
		}
		.register-btn{
			margin-top: 100rpx;
			border-radius: 50rpx;
			border: 2rpx solid #ccc;
		}
	}
</style>